﻿using Flurl.Http;
using Vin.Extension.Framework.Map.Tencent.Models;
using Vin.Extension.Framework.Map.Tencent.Response;
using Vin.Tool.Core.JsonCategory;
using Vin.Tool.Domain.ExceptionModel;

namespace Vin.Extension.Framework.Map.Tencent.Service;

public class VinTencentMapLocationService : BaseTencentMapService
{
    public async Task<WxGeocoderResponse> LocationParseAsync(TencentMapLocationConfig config)
    {
        var request = CreateRequest("/ws/geocoder/v1")
            .SetQueryParam("key", Option.Key)
            .SetQueryParam("location", $"{config.Latitude},{config.Longitude}");

        if (config.GetPoi.HasValue)
        {
            request.SetQueryParam("get_poi", config.GetPoi.Value);
        }

        if (config.PoiOptions.Any())
        {
            request.SetQueryParam("poi_options", string.Join(";", config.PoiOptions));
        }

        var response = await request.GetStringAsync();

        return response.ToUnderLineObject<WxGeocoderResponse>() ?? throw new BusinessException("解析失败");
    }
}